<?php
namespace admin\controllers;



use admin\models\UserRequirements;
use Yii;
use yii\data\Pagination;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;

/**
 * Created by PhpStorm.
 * User: 沈小鱼
 * Date: 2016/7/26
 * Time: 19:37
 */
class RequirementController extends BaseController{



    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['add','list'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                ],
            ],
        ];
    }



    public function actionList(){
        $user_id = Yii::$app->request->get('user_id');
        $requirement = UserRequirements::findOne(['mu_id'=>$user_id]);
        return $this->renderPartial('list',[
            'requirement'=>$requirement,
        ]);
    }


    public function actionAdd(){
        $admin = Yii::$app->user->identity->getId();
        if(empty($admin)){
            return $this->showResult(302,'用户已登出');
        }
        $user_id = Yii::$app->request->get('id');
        if(empty($user_id)){
            return $this->showResult(301,'读取用户信息失败');
        }
        $requirementArr = Yii::$app->request->post('UserRequirements');
        foreach ($requirementArr as $key=>$value){
            $requirementArr[$key] = str_replace(' ','',$value);
        }
        if(empty($requirementArr['mr_id'])){
            $requirement = new UserRequirements();
        }else{
            $requirement = UserRequirements::findOne($requirementArr['mr_id']);
        }
        $requirement->attributes = $requirementArr;
        $requirement->mu_id = $user_id;
        if(!$requirement->save()){
            return $this->showResult(400,'保存失败');
        }else{
            return $this->showResult(200,'保存成功');
        }
    }

}